Skip to content

chore(flat): replace std::map with std::unordered_map for key_id_mapping#153

Merged
iaojnh merged 3 commits intoalibaba:mainfrom
longway-code:chore/flat-key-id-mapping-unordered-map
Feb 28, 2026
Merged

chore(flat): replace std::map with std::unordered_map for key_id_mapping#153
iaojnh merged 3 commits intoalibaba:mainfrom
longway-code:chore/flat-key-id-mapping-unordered-map

Conversation

@longway-code
Copy link
Contributor

key_id_mapping_ is only used for point lookups — no range scans, no ordering needed. std::unordered_map is a better fit.

  • get_id() lookup: O(log N) → O(1)
  • Index load: O(N log N) → O(N)
  • Memory: slightly lower — three tree pointers (parent/left/right) per node replaced by one next pointer plus a bucket array; net ~12MB savings at N=1M

Microbenchmark — isolated map::find vs unordered_map::find, random key order, all hits:

N std::map std::unordered_map speedup
10k 57.3 ns/op 7.0 ns/op 8.2x
100k 88.3 ns/op 10.6 ns/op 8.3x
1M 138.5 ns/op 15.1 ns/op 9.2x

…ing_

Reduces key lookup in get_id() from O(log N) to O(1) average, and index
load time from O(N log N) to O(N). Also lowers per-entry memory usage by
eliminating the three tree-node pointers (parent/left/right).
@longway-code longway-code marked this pull request as ready for review February 20, 2026 11:29
@iaojnh iaojnh self-requested a review February 27, 2026 12:55
@iaojnh
Copy link
Collaborator

iaojnh commented Feb 27, 2026

@greptile

@greptile-apps
Copy link

greptile-apps bot commented Feb 27, 2026

Greptile Summary

This PR optimizes the FlatSearcher by replacing std::map with std::unordered_map for the internal key_id_mapping_ data structure. The change is well-justified and safe:

  • Usage pattern supports the change: The mapping is only used for point lookups via find() and insertions via operator[] — no iteration, no ordering dependencies, no range queries
  • Performance improvement: Reduces lookup complexity from O(log N) to O(1), and index load from O(N log N) to O(N)
  • Type compatibility: Both key_t (uint64_t) and node_id_t (uint32_t) are primitive types with standard hash functions
  • No behavioral changes: The change is purely internal; external API and semantics remain unchanged

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The change is a straightforward container swap with no logic changes. All uses of key_id_mapping_ are compatible with unordered_map (point lookups, insertions, clearing). The types are primitive integers with standard hash functions. The change improves performance without any behavioral side effects.
  • No files require special attention

Important Files Changed

Filename Overview
src/core/algorithm/flat/flat_searcher.h Changed std::map to std::unordered_map for key_id_mapping_ - appropriate optimization for point-lookup-only usage pattern

Last reviewed commit: b41a87a

@iaojnh iaojnh merged commit 4e42031 into alibaba:main Feb 28, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants